﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;

namespace MyStream.Core
{
    public class ShelveService
    {
        

        public ShelveService()
        {
            
        }

        public static ShelveSetsInfos CreateShelveSet(string workspacename, string servername, string shelveSetName)
        {
            TeamFoundationServer _currentServer;
            VersionControlServer _currentTFVC;

            int operationCount = 0;
            int pendingChangesCount = 0;
            foreach(TeamFoundationServer tfs in Microsoft.TeamFoundation.Client.RegisteredServers.GetServers())
            {
                //if (!tfs.HasAuthenticated)
                //    throw new NotConnectedExceptions(tfs.Name, "Unable to retrieve WorkSpaces informations from this server");

                if (tfs.Name == servername)
                {
                    _currentTFVC = tfs.GetService(typeof(VersionControlServer)) as VersionControlServer;
                    Workspace ws = _currentTFVC.GetWorkspace(workspacename, _currentTFVC.AuthenticatedUser);
                    
                    PendingChange[] currentChanges = ws.GetPendingChanges();

                    if(currentChanges != null && currentChanges.Length > 0)
                    {
                        operationCount++;
                        pendingChangesCount += currentChanges.Length;

                        Shelveset shv = new Shelveset(_currentTFVC,shelveSetName,_currentTFVC.AuthenticatedUser);
                        shv.Comment = string.Format(
                            "Automaticaly created by MyStream : based on audit of workspace : {0} at {1} ",
                            workspacename,
                            DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString());

                        ws.Shelve(shv, currentChanges, ShelvingOptions.None);
                    }
                }

            }

            ShelveSetsInfos result = new ShelveSetsInfos();

            result.count = operationCount;
            result.pendingChangesCount = pendingChangesCount;
            return result;
        }

        public static int DeleteOldShelveSets(DateTime olderThan, string commentMark)
        {
            TeamFoundationServer _currentServer;
            VersionControlServer _currentTFVC;

            int deletedShevelsets = 0;
            foreach(TeamFoundationServer tfs in Microsoft.TeamFoundation.Client.RegisteredServers.GetServers())
            {
                    _currentTFVC = tfs.GetService(typeof(VersionControlServer)) as VersionControlServer;

                    Shelveset[] svs = _currentTFVC.QueryShelvesets(null,_currentTFVC.AuthenticatedUser);
                    foreach(Shelveset sv in svs)
                    {
                        if(sv.CreationDate < olderThan && sv.Comment.Contains(commentMark))
                        {
                            deletedShevelsets++;
                            _currentTFVC.DeleteShelveset(sv);
                        }
                    }   
            }
            return deletedShevelsets;
        }
        

    }

    public struct ShelveSetsInfos
    {
        public int count;
        public int pendingChangesCount;
    }
}
